#!/usr/bin/python3
# @Time    : 2018/5/26 下午 14:03:06  
# @Author  : LaoJI
from flask import request
from . import web
from app.forms.questions import GenerateForm
from app.service.generator.app import create_ques
from app.service.generator.model.config import Config


@web.route('/generate', methods=['POST'])
def generate():
    form = GenerateForm(request.form)
    myconf = None
    # if form.validate():
    try:
        datax = request.json
        # request.form 取x-wwww-form-urlencode/form-data 发来的的数据 他们区别 前者多行
        # 后者一行 发出的时候表单默认是x-wwww-form-urlencode
        # 如果是表单提交文件的话需要设置这个multipart/form-data
        # multipart/form-data与x-www-form-urlencoded区别
        # multipart/form-data：既可以上传文件等二进制数据，也可以上传表单键值对，只是最后会转化为一条信息；
        # x-www-form-urlencoded：只能上传键值对，并且键值对都是间隔分开的。
        # request.json 取application/json 格式发来的数据  发出的时候也需要设置这个head头
        maxnumofoper = datax.get("maxnumofoper")
        hasfraction = datax.get("hasfraction")
        hasmulanddiv = datax.get("hasmulanddiv")
        hasparentheses = datax.get("hasparentheses")
        # print(datax.get("maxnumofoper"), datax.get("numofexp"), datax.get("myrange"))
        numofexp = datax.get("numofexp")
        myrange = datax.get("myrange")
        myconf = Config(maxnumofoper=maxnumofoper, hasfraction=hasfraction, hasmulanddiv=hasmulanddiv,
                        hasparentheses=hasparentheses,
                        numofexp=numofexp, myrange=myrange)

        # myconf.hasfraction = form.hasfraction.data.strip()
        ques = create_ques(myconf,"test")
    except Exception as err:
        ques = dict()
        ques["success"] = False
        ques["questions"] = []
        return ques
    ques["success"] = True
    return ques
